﻿using Abp.Authorization;
using shenghua.Authorization;
using shenghua.Basics.Areas;
using shenghua.Basics.AreaSetting.Dto;
using shenghua.Basics.CommodityClasses;
using shenghua.Basics.CommodityClassSetting.Dto;
using shenghua.Basics.CommodityGuarantees;
using shenghua.Basics.CommodityGuaranteeSetting.Dto;
using shenghua.Basics.CommodityWares;
using shenghua.Basics.CommodityWareSetting.Dto;
using shenghua.Basics.MemberFormItems;
using shenghua.Basics.MemberFormItemSetting.Dto;
using shenghua.Basics.Setting.Dto;
using shenghua.Basics.ShippingTemplates;
using shenghua.Basics.ShippingTemplateSetting.Dto;
using shenghua.Basics.WebsiteInfos.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Basics.Setting
{
    public class SettingAppService : shenghuaAppServiceBase, ISettingAppService
    {
        private readonly SettingManager _settingManager;
        private readonly MemberLevels.MemberLevelManager _memberLevelManager;
        private readonly ShoppingOptionParameters.ShoppingOptionParameterManager _shoppingOptionParameterManager;
        private readonly CommoditySpecificationParameters.CommoditySpecificationParameterManager _commoditySpecificationParameterManager;
        private readonly CommodityWares.CommodityWareManager _commodityWareManager;
        private readonly CommodityClassManager _commodityClassManager;
        private readonly AreaManager _areaManager;
        private readonly ShippingTemplateManager _shippingTemplateManager;
        private readonly MemberFormItemManager _memberFormItemManager;
        private readonly CommodityGuaranteeManager _commodityGuaranteeManager;
        //private readonly WebsiteInfoManager _websiteInfoManager;

        public SettingAppService(
            SettingManager settingManager,
            MemberLevels.MemberLevelManager memberLevelManager,
            ShoppingOptionParameters.ShoppingOptionParameterManager shoppingOptionParameterManager,
            CommodityWares.CommodityWareManager commodityWareManager,
            CommoditySpecificationParameters.CommoditySpecificationParameterManager commoditySpecificationParameterManager,
            CommodityClassManager commodityClassManager,
            AreaManager areaManager,
            ShippingTemplateManager shippingTemplateManager,
            MemberFormItemManager memberFormItemManager,
            CommodityGuaranteeManager commodityGuaranteeManager
            //WebsiteInfoManager websiteInfoManager
            )
        {
            _settingManager = settingManager;
            _memberLevelManager = memberLevelManager;
            _shoppingOptionParameterManager = shoppingOptionParameterManager;
            _commodityWareManager = commodityWareManager;
            _commoditySpecificationParameterManager = commoditySpecificationParameterManager;
            _commodityClassManager = commodityClassManager;
            _areaManager = areaManager;
            _shippingTemplateManager = shippingTemplateManager;
            _memberFormItemManager = memberFormItemManager;
            _commodityGuaranteeManager = commodityGuaranteeManager;
            //_websiteInfoManager = websiteInfoManager;
        }

        public async Task<AllSettingDto> GetAllSettings()
        {
            List<CommodityWare> wares = await _commodityWareManager.GetAll();
            List<CommodityWareWithSpecificationParametersDto> wareDtos = new List<CommodityWareWithSpecificationParametersDto>();
            foreach(var ware in wares)
            {
                wareDtos.Add(new CommodityWareWithSpecificationParametersDto
                {
                    Id = ware.Id,
                    Name = ware.Name,
                    SpecificationParameters = ObjectMapper.Map<List<CommoditySpecificationParameterDto>>(await _commoditySpecificationParameterManager.GetAll(ware.Id))
                });
            }

            return new AllSettingDto
            {
                MemberLevels = ObjectMapper.Map<List<MemberLevelSetting.Dto.MemberLevelDto>>(await _memberLevelManager.GetAll()),
                ShoppingOptionParameters = ObjectMapper.Map<List<ShoppingOptionParameterSetting.Dto.ShoppingOptionParameterDto>>(await _shoppingOptionParameterManager.GetAll()),
                CommodityWares = wareDtos,
                CommodityClasses = ObjectMapper.Map<List<WordTreeDto<CommodityClassDto>>>(await _commodityClassManager.GetTree()),
                Areas = ObjectMapper.Map<List<WordTreeDto<AreaDto>>>(await _areaManager.GetTree()),
                ShippingTemplates = (await _shippingTemplateManager.GetAll()).Select(r => new ShippingTemplateDto(r) ).ToList(),
                MemberFormItems = ObjectMapper.Map<List<MemberFormItemDto>>(await _memberFormItemManager.GetAll()),
                CommodityGuarantees = ObjectMapper.Map<List<CommodityGuaranteeDto>>(await _commodityGuaranteeManager.GetAll()),
                //WebsiteInfo = ObjectMapper.Map<WebSiteInfoDto>(_websiteInfoManager.GetWebsiteInfo()),
                Ship = await GetShipSetting(),
                XP = await GetXPSetting(),
                Payment = await GetPaymentSetting()
            };
        }

        [AbpAuthorize(PermissionNames.XPManagement)]
        public async Task SetXPSetting(XPSettingDto input)
        {
            await _settingManager.SetCanGainXPFromConsumption(input.CanGainXPFromConsumption);
            await _settingManager.SetXPGainedByOneAmount(input.XPGainedByOneAmount);
            await _settingManager.SetIsXPGainedByOrderTotalAmount(input.IsXPGainedByOrderTotalAmount);
            await _settingManager.SetIsXPGainedByCommodity(input.IsXPGainedByCommodity);

            await _settingManager.SetCanGainXPFromLogin(input.CanGainXPFromLogin);
            await _settingManager.SetXPGainedByOneLogin(input.XPGainedByOneLogin);

            await _settingManager.SetCanOffsetAmountByXP(input.CanOffsetAmountByXP);
            await _settingManager.SetXPNeededToOffsetOneAmount(input.XPNeededToOffsetOneAmount);
            await _settingManager.SetMaxPercentOfOffsetAmount(input.MaxPercentOfOffsetAmount);
            await _settingManager.SetMaxXPOfOneOrder(input.MaxXPOfOneOrder);
        }
        public async Task<XPSettingDto> GetXPSetting()
        {
            return new XPSettingDto
            {
                CanGainXPFromConsumption = _settingManager.GetCanGainXPFromConsumption(),
                XPGainedByOneAmount = _settingManager.GetXPGainedByOneAmount(),
                IsXPGainedByOrderTotalAmount = _settingManager.GetIsXPGainedByOrderTotalAmount(),
                IsXPGainedByCommodity = _settingManager.GetIsXPGainedByCommodity(),

                CanGainXPFromLogin = _settingManager.GetCanGainXPFromLogin(),
                XPGainedByOneLogin = _settingManager.GetXPGainedByOneLogin(),

                CanOffsetAmountByXP = _settingManager.GetCanOffsetAmountByXP(),
                XPNeededToOffsetOneAmount = _settingManager.GetXPNeededToOffsetOneAmount(),
                MaxPercentOfOffsetAmount = _settingManager.GetMaxPercentOfOffsetAmount(),
                MaxXPOfOneOrder = _settingManager.GetMaxXPOfOneOrder()
            };
        }

        [AbpAuthorize(PermissionNames.ShipManagement)]
        public async Task SetShipSetting(ShipSettingDto input)
        {
            await _settingManager.SetEnabledSendSMSWhenShipped(input.EnabledSendSMSWhenShipped);
            await _settingManager.SetEnabledTrackWuliu(input.EnabledTrackWuliu);
            await _settingManager.SetShippingTemplateType(input.ShippingTemplateType);
        }
        public async Task<ShipSettingDto> GetShipSetting()
        {
            return new ShipSettingDto
            {
                EnabledSendSMSWhenShipped = _settingManager.GetEnabledSendSMSWhenShipped(),
                EnabledTrackWuliu = _settingManager.GetEnabledTrackWuliu(),
                ShippingTemplateType = _settingManager.GetShippingTemplateType()
            };
        }
        
        [AbpAuthorize(PermissionNames.PaymentManagement)]
        public async Task SetPaymentSetting(PaymentSettingDto input)
        {
            await _settingManager.SetCanWeixinPay(input.CanWeixinPay);
            await _settingManager.SetCanAlipay(input.CanAlipay);
            if(input.CanAlipay)
            {
                await _settingManager.SetAlipayConfigInfo(input.AlipayConfigInfo);
            }
            await _settingManager.SetCanBankTransfer(input.CanBankTransfer);
            if(input.CanBankTransfer)
            {
                await _settingManager.SetBankInfos(input.BankInfos);
            }
        }
        public async Task<PaymentSettingDto> GetPaymentSetting()
        {
            return new PaymentSettingDto
            {
                CanWeixinPay = _settingManager.GetCanWeixinPay(),
                CanAlipay = _settingManager.GetCanAlipay(),
                AlipayConfigInfo = _settingManager.GetAlipayConfigInfo(),
                CanBankTransfer = _settingManager.GetCanBankTransfer(),
                BankInfos = _settingManager.GetBankInfos()
            };
        }

        [AbpAuthorize(PermissionNames.MallGeneralManagement)]
        public async Task SetMallGeneralSetting(MallGeneralSettingDto input)
        {
            await _settingManager.SetInventoryDeductionType(input.InventoryDeductionType);
            await _settingManager.SetShoppingVisibleType(input.ShoppingVisibleType);
        }
        public async Task<MallGeneralSettingDto> GetMallGeneralSetting()
        {
            return new MallGeneralSettingDto
            {
                InventoryDeductionType = _settingManager.GetInventoryDeductionType(),
                ShoppingVisibleType = _settingManager.GetShoppingVisibleType()
            };
        }
    }
}
